package com.wfg.common;

import com.wfg.common.balance.SimpleLoadBalance;
import com.wfg.common.msg.Invocation;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;

/**
 * @author wufagang
 * @description
 * @date 2023年05月11日 08:11
 */
public class ProxyFactory<T> {

    public static <T> T getProxy(final Class interfaceClass){
       return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Invocation invocation = new Invocation(interfaceClass.getName(), method.getName(), method.getParameterTypes(), args);
                List<Url> urlList=RemoteMapRegister.get(interfaceClass.getName());
                Url url = SimpleLoadBalance.random(urlList);
                Protocal protocol = ProtoaclFactory.getProtocol();
                String result= protocol.send(url,invocation);
                return result;
            }
        });
    }
}
